package com.shop.service.impl;

import com.shop.dao.SalesUserDao;
import com.shop.entity.AdminUser;
import com.shop.entity.SalesUser;
import com.shop.service.SalesUserService;
import com.shop.service.exception.LoginException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class SalesUserServiceImpl implements SalesUserService {
    @Autowired
    private SalesUserDao salesUserDao;

    @Override
    public SalesUser findById(int id) {
        return salesUserDao.getOne(id);
    }

    @Override
    public Page<SalesUser> findAll(Pageable pageable) {
        return salesUserDao.findAll(pageable);
    }

    @Override
    public List<SalesUser> findAllExample(Example<SalesUser> example) {
        return salesUserDao.findAll(example);
    }

    @Override
    public void update(SalesUser salesUser) {
        salesUserDao.save(salesUser);
    }

    @Override
    public int create(SalesUser salesUser) {
        return salesUserDao.save(salesUser).getId();
    }

    @Override
    public void delById(int id) {
        salesUserDao.delete(id);
    }

    /**
     * 根据用户名查询
     *
     * @param username
     * @return
     */
    @Override
    public List<SalesUser> findByUsername(String username) {
        return salesUserDao.findByUsername(username);
    }

    /**
     * 检查登录
     *
     * @param username
     * @param password
     * @return
     */
    @Override
    public SalesUser checkLogin(HttpServletRequest request,String username, String password) {
        SalesUser salesUser = salesUserDao.findByUsernameAndPassword(username, password);
        if (salesUser != null) {
            request.getSession().setAttribute("sales_login_user", salesUser);
        } else {
            throw new LoginException("用户名或密码错误");
        }
        return salesUser;
    }

}
